package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //查询所有角色
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    //根据id查询角色
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    //根据角色id查询角色下的权限id
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        return roleDao.findPermissionIdsByRoleId(id);
    }

    //根据角色id查询角色下的菜单id
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        return roleDao.findMenuIdsByRoleId(id);
    }


    //添加角色
    public void add(Map map) {
        List<Integer> permissionIds = (List<Integer>) map.get("param1");
        List<Integer> menuIds = (List<Integer>) map.get("param2");
        Map map1 = (Map) map.get("param3");
        Role role = new Role();
        role.setName((String)map1.get("name"));
        role.setKeyword((String)map1.get("keyword"));
        role.setDescription((String)map1.get("description"));


        //新增角色表，操作t_role表
        roleDao.add(role);
        //设置角色和权限的多对多的关联关系，操作t_role_permission表
        //设置角色和权限的多对多的关联关系，操作t_role_menu表
        Integer roleId = role.getId();
        this.setRoleAndPermission(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);
    }

    //编辑角色
    public void edit(Map map) {
        List<Integer> permissionIds = (List<Integer>) map.get("param1");
        List<Integer> menuIds = (List<Integer>) map.get("param2");
        Map map1 = (Map) map.get("param3");
        Role role = new Role();
        role.setName((String)map1.get("name"));
        role.setKeyword((String)map1.get("keyword"));
        role.setDescription((String)map1.get("description"));
        role.setId((Integer) map1.get("id"));

        //修改角色基本信息，操作菜单表
        roleDao.edit(role);
        //清理当前检查组关联的检查项，操作中间关系表t_role_permission表
        //清理当前检查组关联的检查项，操作中间关系表t_role_menu表
        Integer roleId = role.getId();
        roleDao.deletePermissionByRoleId(roleId);
        roleDao.deleteMenuByRoleId(roleId);
        //重新建立当前角色和菜单的关联关系
        this.setRoleAndPermission(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);
    }

    //删除角色
    public void deleteById(Integer id) {
        //判断当前角色是否已经关联到权限、菜单、用户
        long count = roleDao.findPermissionCountByroleId(id);
        long count2 = roleDao.findMenuCountByroleId(id);
        long count3 = roleDao.findUserCountByroleId(id);
        if (count > 0 || count2 > 0 || count3>0) {
            //当前角色已经被关联到权限、菜单、用户，不允许删除
            new RuntimeException();
        }
        roleDao.deleteById(id);

    }

    private void setRoleAndPermission(Integer roleId, List<Integer> permissionIds) {
        if (permissionIds != null && permissionIds.size() > 0) {
            for (Integer permission : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permission);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    private void setRoleAndMenu(Integer roleId, List<Integer> menuIds) {
        if (menuIds != null && menuIds.size() > 0) {
            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("menuId", menuId);
                roleDao.setRoleAndMenu(map);
            }
        }
    }

}
